package com.ews.onlineexchange.service;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.TradeStatus;

@Model
public class AudittedAndNotOverBulletinList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AudittedAndNotOverBulletinList() {
		setOrder( "auditTime DESC" );
	}

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<Bulletin> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		restrictions.add( cb.equal( entity.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		restrictions.add( cb.isTrue( entity.<Boolean>get( "auditResult" ) ) );
		restrictions.add( cb.isFalse( entity.<Boolean>get( "cancelled" ) ) );
		restrictions.add( cb.equal( entity.get( "tradeStatus" ), TradeStatus.Opened ) );
		String projectName = (String) getParameterValues().get( "projectName" );
		if ( !StringUtils.isBlank( projectName ) )
			restrictions.add( cb.like( entity.get( "projectName" ), getStringRestriction( projectName ) ) );

		String serialNumber = (String) getParameterValues().get( "serialNumber" );
		if ( !StringUtils.isBlank( serialNumber ) )
			restrictions.add( cb.like( entity.get( "serialNumber" ), getStringRestriction( serialNumber ) ) );
		return restrictions;
	}

}
